package com.ruoyi.file.router

import com.ruoyi.file.properties.AliOssConfigProperties
import com.ruoyi.file.util.compressionVideo
import cn.hutool.core.io.FileUtil
import cn.hutool.core.util.RandomUtil
import com.ruoyi.file.util.FileUploadHandler
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.stereotype.Service

@Service
@EnableConfigurationProperties(AliOssConfigProperties::class)
class FileCompressService {

    @Autowired
    private lateinit var fileUploadHandler: FileUploadHandler

    fun compress(url: String): String =
        RandomUtil.randomNumbers(10)
            .let { traceId ->
                val uploadUrl = fileUploadHandler.download(url, traceId)
                val outUrl = uploadUrl.compressionVideo(traceId)
                fileUploadHandler.upload(FileUtil.file(outUrl), traceId)
            }
}